package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private EmpService empService;
    @Override
    public PageResult LimitSelect(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        Page<Clazz> p = new Page<>(page, pageSize);
        p=lambdaQuery().like(name!=null&&!"".equals(name),Clazz::getName,name)
                .ge(begin!=null,Clazz::getBeginDate,begin)
                .le(end!=null,Clazz::getEndDate,end)
                .page(p);
        //把查询到的结果封装到list集合
        List<Clazz> clazzs = p.getRecords();
        //拿到所有员工的部门id
        if (clazzs!=null&&clazzs.size()>0) {
            List<Long> empId = empService.list().stream().map(Emp::getId).toList();
            //调用empService方法查询部门名称
            List<Emp> emps = empService.listByIds(empId);
            //遍历员工集合，把部门id对应的部门名称设置到Emp对象中
            clazzs.forEach(cla -> {
                emps.forEach(emp -> {
                    if (emp.getId() != null && emp.getId().equals(cla.getMasterId())) {
                        cla.setMasterName(emp.getName());
                    }
                });
            });
        }
        //现在时间
        LocalDate now = LocalDate.now();
        clazzs.stream().map(Clazz::getBeginDate).toList().forEach(beginDate -> {
            if (beginDate.isBefore(now)){
            }
        });
        clazzs.stream().map(Clazz::getEndDate).toList();




        return new PageResult<>(p.getTotal(),p.getRecords());
    }
}
